/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package grafrajz.dialogs;

import grafrajz.GrafRajzWindow;
import grafrajz.GraphLogic;
import grafrajz.utils.Node;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author adam_fejes_dell
 * Új él létrehozó párbeszédablak
 */
public class NewEdgeWindow extends JDialog{
    private JButton okBtn, cancelBtn;
    private JTextField edgeWeight;
    private JList sourceNodes, destinationNodes;
    private JPanel nodesPanel, buttonsPanel;
    private DefaultListModel nodesModel;
    private JScrollPane scroll;
    
    
    public NewEdgeWindow(final GrafRajzWindow frame, final GraphLogic gLogic) {
        super(frame);
        setTitle("Create new edge");
        setLayout(new GridLayout(3, 1));
        
        setLocationRelativeTo(frame);
        setSize(new Dimension(300, 300));
        
        edgeWeight = new JTextField();
        edgeWeight.setMaximumSize(new Dimension(300, 50));
        if(gLogic.isWeightened())
            add(edgeWeight);
        
        nodesPanel = new JPanel();
        nodesPanel.setLayout(new GridLayout(1, 2, 5, 5));
        nodesPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        nodesModel = new DefaultListModel();
        ArrayList<Node> nodes = gLogic.getAllNodes();
        
        for(Node n : nodes){
            nodesModel.addElement(n);
        }
        sourceNodes = new JList(nodesModel);
        scroll = new JScrollPane(sourceNodes);
        nodesPanel.add(scroll);
        
        destinationNodes = new JList(nodesModel);
        scroll = new JScrollPane(destinationNodes);
        nodesPanel.add(scroll);
        add(nodesPanel);

        
        buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new BorderLayout());
        
        
        
        okBtn = new JButton("OK");
        okBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(destinationNodes.getSelectedIndex() != -1 && sourceNodes.getSelectedIndex() != -1 && sourceNodes.getSelectedValue() != destinationNodes.getSelectedValue()){
                    if(gLogic.isWeightened() && !edgeWeight.getText().toString().equals("")){
                        gLogic.addEdge(sourceNodes.getSelectedValue().toString(), Integer.parseInt(edgeWeight.getText().toString()), destinationNodes.getSelectedValue().toString());
                        frame.updateWindow(frame);
                        dispose();
                    }
                    else if(!gLogic.isWeightened()){
                        gLogic.addEdge(sourceNodes.getSelectedValue().toString(), destinationNodes.getSelectedValue().toString());
                        frame.updateWindow(frame);
                        dispose();
                    }
                }
            }
        });
        buttonsPanel.add(okBtn, BorderLayout.WEST);
        
        cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        buttonsPanel.add(cancelBtn, BorderLayout.EAST);
        add(buttonsPanel);

    }
}
